<?php


namespace App\HttpController\Admin;



use App\Consts\RedisKey;
use App\Middleware\AuthMiddleware;
use App\Utility\Response;
use App\Model\Authority;
use EasySwoole\RedisPool\Redis;
class Role extends AdminBaseController
{

    public function index(){

        $list = \App\Model\Role::create()->where('id',1,'>')->all();


        foreach ($list as &$item) {
            $item = $item->toArray();
            $item['authority'] = explode(',',$item['authority_ids']);
        }
        return $this->returnData(200,$list);

    }

    public function create(){
        $params = $this->request()->getRequestParam();

        $res =  \App\Model\Role::create()->data($params)->save();
        if($res){

            Redis::defer('redis')->set(RedisKey::user_authority_list($res),
                json_encode(AuthMiddleware::getInstance()->getAuthority($res))
            );
            return $this->returnData(200);
        }
        return $this->returnData(400);
    }


    public function edit(){
        $params = $this->request()->getRequestParam();
        $id = $params['id'];
        unset($params['id']);
        $res =  \App\Model\Role::create()->where('id',$id)->update($params);
        if($res){
            Redis::defer('redis')->set(RedisKey::user_authority_list($id),
                json_encode(AuthMiddleware::getInstance()->getAuthority($id))
            );
            return  $this->returnData(200);
        }
        return $this->returnData(400);

    }

}